package util;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.Map;
import java.util.TreeMap;

import javax.imageio.ImageIO;

public class ImageNudeDetect {
								 	//R  G  B
	public int colorA = 7944996; 	//79 3B 24
	public int colorB = 16696767;	//FE C5 BF
	
	private Map<Character, Integer> arA = new TreeMap<Character, Integer>();
	private Map<Character, Integer> arB = new TreeMap<Character, Integer>();
	
	public ImageNudeDetect () {
		arA.put('R', (colorA >> 16) & 0xFF);
		arA.put('G', (colorA >> 8) & 0xFF);
		arA.put('B', colorA & 0xFF);
		
		arB.put('R', (colorB >> 16) & 0xFF);
		arB.put('G', (colorB >> 8) & 0xFF);
		arB.put('B', colorB & 0xFF);
	}
	
	
	public int getSource(String urlImage) {
		try {
			URL url = new URL(urlImage);
			BufferedImage image = ImageIO.read(url);
			int x = image.getWidth();
			int y = image.getHeight();
			double score = 0;
			
//			int c = image.getRGB(x,y);
//			int  red = (c & 0x00ff0000) >> 16;
//			int  green = (c & 0x0000ff00) >> 8;
//			int  blue = c & 0x000000ff;
//			System.out.println(green);
			
			int[] xPoints = {x/8, x/4, (x/8 + x/4), x-(x/8 + x/4), x-(x/4), x-(x/8)};
			int[] yPoints = {y/8, y/4, (y/8 + y/4), y-(y/8 + y/4), y-(y/8), y-(y/8)};
			int[] zPoints = {xPoints[2], yPoints[1], xPoints[3], y};
			
			for(int i=1; i<x; i++)
	        {
	            for(int j=1; j<y; j++)
	            {
	              
	                int pix = image.getRGB(i, j);
	                int r = (pix >> 16) & 0xFF;
	                int g = (pix >> 8) & 0xFF;
	                int b = pix & 0xFF;
	                Color c = new Color(r,g,b,0); 
	                
	                int color = c.getRGB();
	                if(color >= colorA && color <= colorB)
	                {
	                	Map<Character, Integer> color2 = new TreeMap<Character, Integer>();
	                	color2.put('R', (color >> 16) & 0xFF);
	                	color2.put('G', (color >> 8) & 0xFF);
	                	color2.put('B', color & 0xFF);
	                	
	                    if(color2.get('G') >= arA.get('G') && color2.get('G') <= arB.get('G') && color2.get('B') >= arA.get('B') && color2.get('B') <= arB.get('B'))
	                    {
	                        if(i >= zPoints[0] && j >= zPoints[1] && i <= zPoints[2] && j <= zPoints[3])
	                        {
	                            score += 3;
	                        }
	                        else if(i <= xPoints[0] || i >= xPoints[5] || j <= yPoints[0] || j >= yPoints[5])
	                        {
	                            score += 0.10;
	                        }
	                        else if(i <= xPoints[0] || i >= xPoints[4] || j <= yPoints[0] || j >= yPoints[4])
	                        {
	                            score += 0.40;
	                        }
	                        else
	                        {
	                            score += 1.50;
	                        }
	                    }
	                }
	            }
	        }
			
			score =  (score * 100) / ((double) x * (double) y);
			System.out.println(score);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
		return 0;
	}
	
	public static void main(String[] args) {
		ImageNudeDetect imageNudeDetect = new ImageNudeDetect();
		imageNudeDetect.getSource("http://media.bizwebmedia.net/sites/34794/data/images/2013/8/5312271quan_nam_mns36_c.jpg");
	}
}
